package cn.hd01.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hd01.repository.entity.Product;
import cn.hd01.service.OrderProductService;
import cn.hd01.service.ProductService;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.util.WebException;

@Controller
@RequestMapping("/product")
@Auth
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private OrderProductService orderProductService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model m, HttpServletRequest request) {
		//查询所有父类产品
		List<Product> parProductList = productService.findParentProductList();
		m.addAttribute("parProductList", parProductList);
		return "product";
	}

	@ResponseBody
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	private Page<Product> find(Product s, @PageableDefault Pageable pageable) {
		Sort sort = new Sort(Direction.DESC, "createTime");
		Pageable page = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
		Page<Product> pageProduct = productService.findAll(s, page);
		List<Product> productList = pageProduct.getContent();
		for (Product product : productList) {
			Integer parentId = product.getParentId();
			if(StringUtils.isEmpty(parentId)) continue;
			Product parentProduct = productService.findOne(parentId);
			if(parentProduct != null) product.setParentName(parentProduct.getName());
		}
		return pageProduct; 
	}

	@ResponseBody
	@RequestMapping(path = "/delete/{id}", method = RequestMethod.GET)
	private void delete(@PathVariable("id") Integer id) {
		productService.delete(id);
	}

	@ResponseBody
	@RequestMapping(path = "/save", method = RequestMethod.POST)
	private void save(Product product) {

		validateProduct(product);
		
		if(product.getId() == null){//保存
			
			Date date = new Date();
			product.setCreateTime(date);
			product.setUpdateTime(date);
			productService.save(product);
			
		}else {//更新
			productService.updateProduct(product);
			orderProductService.updateProductName(product.getName(), product.getId());
		}
	}
	
	private void validateProduct(Product product){
		
		if(StringUtils.isEmpty(product.getName())){
			throw new WebException(HttpStatus.BAD_REQUEST, "名字不能为空");
		}
		if(product.getParentId()!= null && product.getPrice() == null){
			throw new WebException(HttpStatus.BAD_REQUEST, "价格不能为空");
		}
	}
	
	@ResponseBody
	@RequestMapping(path = "/getDetail/{id}", method = RequestMethod.GET)
	public Product getDetail(@PathVariable("id") Integer id) {
		Product product = productService.findOne(id);
		Product parentProduct = productService.findOne(product.getParentId());
		if(parentProduct != null) product.setParentName(parentProduct.getName());
		return product;
	}
	
}
